﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TestAttack : MonoBehaviour
{
    Animation anim;


    public List<AnimationState> animStateList = new List<AnimationState>();

    public string AtkAnimName = "attack";
    public int AtkAnimCount = 5;
    public string IdleAnimName = "idle1";

    public AnimationState _AnimState = null;

    void Start()
    {
        anim = this.GetComponent<Animation>();

        //for (int i = 1; i <= AtkAnimCount; i++)
        //{
        //    AnimationState state = anim[AtkAnimName + i];
        //    if (state)
        //    {
        //        animStateList.Add(state);
        //    }
        //}

        //StartCoroutine(UpdateStack());
    }


    void OnGUI()
    {
        if (GUI.Button(new Rect(0, 0, 300, 80), "以state.length为间隔播放攻击动画"))
        {
            StartCoroutine(StartAttack_以动画时间为间隔());
        }
    }

    IEnumerator StartAttack_以动画时间为间隔()
    {
        yield return StartCoroutine(PlayOnce());

        //yield return new WaitForSeconds(1.0f);

        //yield return StartCoroutine(PlayOnce());

        //yield return new WaitForSeconds(1.0f);
    }

    IEnumerator PlayOnce()
    {
        for (int i = 1; i <= AtkAnimCount; i++)
        {
            string name = AtkAnimName + i;
            anim.Stop();
            _AnimState = anim.PlayQueued(name, QueueMode.PlayNow, PlayMode.StopAll);
           // anim.clip.name = name;
            //anim.Play(name, PlayMode.StopAll);
            //Debug.Log("anim.Play(" + state.name + "), time=" + Time.realtimeSinceStartup);
            yield return new WaitForSeconds(_AnimState.length);

            //每次播完记录位置
            ApplyRootMotion arm = this.GetComponent<ApplyRootMotion>();
            arm.RecordRootPos();
        }

        //anim.PlayQueued

        Debug.Log("111111, time = " + Time.realtimeSinceStartup);
        //anim.Stop(AtkAnimName+(AtkAnimCount-1));

        anim.Play(IdleAnimName);
    }
}
